package com.hsogoo.avatar.serialization;

import java.util.Iterator;
import java.util.ServiceLoader;

import org.apache.commons.lang.StringUtils;

import com.hsogoo.avatar.serialization.hessian.HessianSerializer;
import com.hsogoo.avatar.serialization.jdk.JdkSerializer;
import com.hsogoo.avatar.serialization.json.JsonSerializer;
import com.hsogoo.avatar.serialization.proto.ProtoStuffSerializer;

/**
 * @Author huangsaigang
 * Created @2018/9/25.
 */
public class SerializerFactory {

    public static Serializer getSerializer(){
        ServiceLoader<Serializer> loader = ServiceLoader.load(Serializer.class);
        Iterator<Serializer> iterator = loader.iterator();
        if (!iterator.hasNext()) {
            throw new IllegalStateException(String.format(
                    "no implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!",
                    Serializer.class.getName()));
        }
        return iterator.next();
    }

    public static Serializer getSerializer(byte serializerCode){

        switch (serializerCode){
            case 0X01 :
                return new JdkSerializer();
            case 0X02 :
                return new JsonSerializer();
            case 0X03 :
                return new HessianSerializer();
            case 0X04 :
                return new ProtoStuffSerializer();
        }
        //return default serializer
        return getSerializer();
    }

}
